﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Configuration;
using System.Data.SqlClient;

/// <summary>
/// Summary description for D_PurchaseOrder
/// </summary>
namespace Bestlife.ERP.DataAccess
{
    public class D_PurchaseOrder
    {
        public D_PurchaseOrder()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public static decimal getprice(string pro)
        {
            decimal contact = 0;
            SqlDataReader rdR;
            try
            {
                DateTime date = System.DateTime.Now;


                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                String Ssql = "SELECT  [Pro_Price] FROM [Tbl_Product] where [Pro_ID]='" + pro + "'";
                rdR = clsCon.ReadTable(Ssql);


                if (rdR.Read())
                {
                    contact = (decimal)rdR["Ret_Price"];

                }
                rdR.Close();
                clsCon.conn_new.Close();
            }

            catch
            {
            }
            return contact;



        }

        public static Decimal linetotal(decimal qty, decimal unitprice)
        {
            decimal line_total = qty * unitprice;
            return line_total;
        }

        public static Boolean insert(Bestlife.ERP.Entities.E_PurchaseOrder order, string user)
        {
            Boolean status = false;
            try
            {

                DateTime date = System.DateTime.Now;

                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                string result = getproname(order.Proname);
                string sql = "INSERT INTO [Tbl_ProductOrder]([Or_ID],[Pro_Name],[Pur_Price],[Description],[Qty],[Date_Create],[User_Create]) VALUES ('" + order.Or_ID1 + "','" + result + "','" + order.Unitvalue + "','" + order.Description + "','" + order.Qty + "','" + date + "','" + user + "')";

                Boolean add = Bestlife.ERP.DataAccess.clsCon.AddEditDel(sql);

                if (add)
                {
                    status = true;


                }
                clsCon.conn_new.Close();
            }
            catch (Exception)
            {

            }
            return status;

        }

        public static string getproname(string pro)
        {
            SqlDataReader rdR;
            try
            {
                DateTime date = System.DateTime.Now;
                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);

                String Ssql = "SELECT [Pro_Name] FROM [Tbl_Product] where [Pro_ID]='" + pro + "'";


                rdR = clsCon.ReadTable(Ssql);

                if (rdR.Read())
                {
                    string proname = rdR["Pro_Name"].ToString();
                    rdR.Close();
                    return proname;
                }
                rdR.Close();
                clsCon.conn_new.Close();
            }
            catch
            {
            }

            string error = "error";
            return error;

        }

        public static void insertorder(Bestlife.ERP.Entities.E_PurchaseOrder order)
        {
            SqlDataReader rdR;
            try
            {
                DateTime date = System.DateTime.Now;

                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                string sql1 = "select count(Pro_Name) as count from Tbl_ProductOrder where Or_ID='" + order.Or_ID1 + "'";
                rdR = clsCon.ReadTable(sql1);

                if (rdR.Read())
                {
                    int count = (int)rdR["count"];
                    rdR.Close();
                    string sql = "INSERT INTO [BestlifeDB].[dbo].[Tbl_Order]([Or_ID],[Oder_Date],[Due_Date],[Date_Create],[TotalProduct]) VALUES('" + order.Or_ID1 + "','" + order.Issu + "','" + order.Due + "','" + date + "','" + count + "')";
                    Bestlife.ERP.DataAccess.clsCon.AddEditDel(sql);

                }
                rdR.Close();
                clsCon.conn_new.Close();
            }
            catch
            {
            }

        }

        public static void insertsupporder(Bestlife.ERP.Entities.E_PurchaseOrder sup, string user)
        {
            try
            {
                DateTime date = System.DateTime.Now;

                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                string sql = "INSERT INTO [BestlifeDB].[dbo].[Tbl_SupplierOrder]([Sup_Name],[Or_ID],[Contact],[Date_Create],[User_Create])VALUES('" + sup.Supplier + "','" + sup.Or_ID1 + "','" + sup.Contrat + "','" + date + "','" + user + "')";
                Boolean add = Bestlife.ERP.DataAccess.clsCon.AddEditDel(sql);
                clsCon.conn_new.Close();
            }
            catch (Exception)
            {
            }
        }

        public static Boolean total(Bestlife.ERP.Entities.E_PurchaseOrder tot, string user)
        {
            Boolean status = false;
            try
            {

                DateTime date = System.DateTime.Now;

                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);

                string sql = "UPDATE [BestlifeDB].[dbo].[Tbl_Order] SET [Total] ='" + tot.Total + "',[Last_Modify] ='" + date + "',[User_Create]='" + user + "'   WHERE Or_ID='" + tot.Or_ID1 + "' ";
                Boolean edit = Bestlife.ERP.DataAccess.clsCon.AddEditDel(sql);
                if (edit)
                {
                    status = true;

                }
                clsCon.conn_new.Close();
            }
            catch (Exception)
            {
            }
            return status;
        }

        public static Bestlife.ERP.Entities.E_PurchaseOrder getbvpv(Bestlife.ERP.Entities.E_PurchaseOrder pro)
        {
            
            SqlDataReader rdR;
            try
            {
                DateTime date = System.DateTime.Now;


                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                String Ssql = "SELECT  [Business_Value],[Point_Value] FROM [Tbl_Product] where [Pro_ID]='" + pro.Proname + "'";
                rdR = clsCon.ReadTable(Ssql);


                if (rdR.Read())
                {
                    pro.Bv = (decimal)rdR["Business_Value"];
                    pro.Pv = (decimal)rdR["Point_Value"];

                }
                rdR.Close();
                clsCon.conn_new.Close();
            }

            catch
            {
            }
            return pro;


        }
    }
}
    
   
